//
//  RYTijiaoView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/13.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import YYText

class RYTijiaoView: SYBaseView {

    let wuping: RYWupingView = {
        let v = RYWupingView()
        return v
    }()
    
    let numTitle: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoFontSize()
        v.text = "认养数"
        
        return v
    }()
    
    let numLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoFontSize()
        v.text = "1"
        v.textAlignment = .center
        return v
    }()
    
    let price: YYLabel = {
        let v:YYLabel = YYLabel()
        v.textColor = UIColor(hex: 0x1D1D1D)
        v.font = 16.autoFontSize()
        
        
        return v
    }()
    
    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0xFEF9F1)
        v.layer.cornerRadius = 3
        return v
    }()
    
    let jian: UIButton = {
        let v: UIButton  = UIButton()
        
        v.setImage(UIImage(named: "jian_ry"), for: .normal)
        return v
    }()
    
    let jia: UIButton = {
        let v: UIButton  = UIButton()
        
        v.setImage(UIImage(named: "jia_ry"), for: .normal)
        return v
    }()
    
    let jianBgPic: UIButton = {
        let v: UIButton  = UIButton()
        
        v.setImage(UIImage(named: "jianhao_bg"), for: .normal)
        return v
    }()
    let jiaBgPic: UIButton = {
        let v: UIButton  = UIButton()
        
        v.setImage(UIImage(named: "jiahao_bg"), for: .normal)
        return v
    }()

    let jingao: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "tanhao")
        return v
    }()
    
    let quan: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "youhuiquan")
        return v
    }()
    
    let jinggaoLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0xFFA800)
        v.font = 13.autoFontSize()
        v.text = "温馨提示："
        
        return v
    }()
    
    let jinggaoDetailLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0xFFA800)
        v.font = 13.autoFontSize()
        v.text = "认养成功之后，您将获得地块当季生产的作物，以及以下赠品："
        v.numberOfLines = 0
        return v
    }()
    
    let quanLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 13.autoFontSize()
        
        return v
    }()
    
    
    func configure(_ model: RYProductDetailModel) {
        let firStr = "￥"
        let d: Double = Double(model.adoptPrice ?? "0") ?? 0.0
        let priceStr = String(format: "%.2f", d)
        let totalStr = "￥\(priceStr)"
        let attString = NSMutableAttributedString(string: totalStr)
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: 0, length: firStr.count))
        attString.yy_setFont(13.autoFontSize(), range: NSRange(location: 0, length:firStr.count))
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setFont(16.autoFontSize(), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        price.attributedText = attString
        quanLb.text = model.present
        wuping.configure(model)
    }

    override func initViews() {
        
        jianBgPic.addTarget(self, action: #selector(jianAction), for: .touchUpInside)
        jiaBgPic.addTarget(self, action: #selector(jiaAction), for: .touchUpInside)
        
        self.backgroundColor = .white
        addSubview(wuping)
        addSubview(numTitle)
        addSubview(numLb)
        addSubview(price)
        
        addSubview(jianBgPic)
        addSubview(jiaBgPic)
        jianBgPic.addSubview(jian)
        jiaBgPic.addSubview(jia)
        
        addSubview(bgView)
        bgView.addSubview(jingao)
        bgView.addSubview(quan)
        bgView.addSubview(jinggaoLb)
        bgView.addSubview(quanLb)
        bgView.addSubview(jinggaoDetailLb)
        wuping.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(100.autoWidth())
        }
        numTitle.snp.makeConstraints { (make) in
            make.top.equalTo(wuping.snp.bottom).offset(13.autoWidth())
            make.left.equalToSuperview().offset(18.autoWidth())
            make.height.equalTo(15.autoWidth())
        }
        numLb.snp.makeConstraints { (make) in
            make.top.equalTo(numTitle.snp.top).offset(8.autoWidth())
            make.right.equalToSuperview().offset(-50.autoWidth())
            make.height.equalTo(35.autoWidth())
            make.width.equalTo(64.autoWidth())
        }
        price.snp.makeConstraints { (make) in
            make.top.equalTo(numTitle.snp.bottom).offset(12.autoWidth())
            make.left.equalToSuperview().offset(18.autoWidth())
            make.height.equalTo(17.autoWidth())
        }
        jianBgPic.snp.makeConstraints { (make) in
            make.top.equalTo(numTitle.snp.top).offset(8.autoWidth())
            make.right.equalToSuperview().offset(-114.autoWidth())
            make.height.equalTo(35.autoWidth())
            make.width.equalTo(35.autoWidth())
        }
        jiaBgPic.snp.makeConstraints { (make) in
            make.top.equalTo(numTitle.snp.top).offset(8.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(35.autoWidth())
            make.width.equalTo(35.autoWidth())
        }
        jian.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(13.autoWidth())
        }
        jia.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(13.autoWidth())
        }
        
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(wuping.snp.bottom).offset(83.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(110.autoWidth())
        }
        jingao.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.height.width.equalTo(18.autoWidth())
        }
        jinggaoLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview().offset(31.autoWidth())
            make.height.equalTo(18.autoWidth())
        }
        jinggaoDetailLb.snp.makeConstraints { (make) in
            make.top.equalTo(jinggaoLb.snp.bottom).offset(7.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            
        }
        quan.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-12.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.height.equalTo(18.autoWidth())
            make.width.equalTo(21.autoWidth())
        }
        quanLb.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-12.autoWidth())
            make.left.equalToSuperview().offset(36.autoWidth())
            make.height.equalTo(18.autoWidth())
        }
        
        setSearchView()
    }
    
    func setSearchView() {
        
        let renyang = UILabel()
        renyang.text = "立即认养"
        renyang.textColor = .white
        renyang.font = 18.autoFontSize()
        //mark label在button上不用设置该属性
//        renyang.isUserInteractionEnabled = true
        
        let zixun: UIButton = UIButton()
        zixun.setImage(UIImage(named: "lijirenyang_bg"), for: .normal)
        zixun.addAction {[weak self] in
            if let `self` = self {
                
                let currentVC = (self.currentVC as! RYTijiaoViewController)
                let vc = RYQuerenDIngdanViewController(params: currentVC.model, num: Int(self.numLb.text ?? "0") ?? 0)
                self.currentVC.navigationController?.pushViewController(vc, animated: true)
            }
        }
        addSubview(zixun)
        zixun.addSubview(renyang)
        zixun.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-30.autoWidth() - STATUSBAR_HEIGHT)
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.left.equalToSuperview().offset(autoWidth(15.0))
            make.height.equalTo(44.autoWidth())
        }
        renyang.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(44)
        }
    }
    
    @objc func jianAction() {
        
            let num = Int(numLb.text!)! - 1
        if num > 0 {
            numLb.text = "\(num)"
        }
            
    }
    @objc func jiaAction() {
        let maxNum = (self.currentVC as! RYTijiaoViewController).maxNum
            let num = Int(numLb.text!)! + 1
        if maxNum >= num {
            numLb.text = "\(num)"
        } else {
            self.currentVC.view.makeToast(Constant_Toast.ry_buy_max)
        }
            
    }

}
